package edu.polsl.bio.str.manager.hibernate;

import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * //todo class description
 * <p/>
 * Created on: Jul 14, 2011 10:32:12 PM <br/>
 * <a href="http://www.polsl.pl">www.polsl.pl</a>
 *
 * @author lukasz.olczak@polsl.pl
 */
public class LockingMySql5Dialect extends MySQL5Dialect {
  /**
   * slf4j Logger.
   */
  private static final Logger log = LoggerFactory.getLogger(LockingMySql5Dialect.class.getName());

  public LockingMySql5Dialect() {
    super();
    registerFunction( "TIMESTAMPDIFF", new SQLFunctionTemplate( Hibernate.STRING, " TIMESTAMPDIFF(?1, ?2, ?3)" ) );
  }

  @Override
  public String getForUpdateString(LockOptions lockOptions) {
    LockMode lockMode = lockOptions.getLockMode();

    if (lockMode.equals(LockMode.NONE) && lockOptions.getAliasLockCount() > 0) {
      return getForUpdateString();
    }
    return super.getForUpdateString(lockOptions);
  }
}
